#!/bin/bash
#set -x # for DEBUGGING

# Script modified from xiaomi-pro's repo: https://github.com/daliansky/XiaoMi-Pro-Hackintosh/tree/master/one-key-hidpi

DISPLAYPATH="/Library/Displays/Contents/Resources/Overrides"

# Choose option
function choice() {
    choose=0
    echo '(1) Enable HiDPI'
    echo '(2) Disable HiDPI'
    echo '(3) Exit'
    read -p "Enter your choice [1~3]: " choose
}

function networkWarn(){
    echo "ERROR: Fail to download, please check the network state."
    clean
    exit 1
}

function download(){
    echo 'Downloading display files...'
    mkdir -p /tmp/one-key-hidpi
    cd /tmp/one-key-hidpi
    curl -fsSL https://gitee.com/kirainmoe/static-files/raw/master/Icons.plist -O || networkWarn
    curl -fsSL https://gitee.com/kirainmoe/static-files/raw/master/DisplayVendorID-9e5/DisplayProductID-747 -O || networkWarn
    curl -fsSL https://gitee.com/kirainmoe/static-files/raw/master/DisplayVendorID-9e5/DisplayProductID-747.tiff -O || networkWarn
    echo 'Download complete'
    echo
}

function removeold() {
    echo 'Removing previous version...'
    # Remove previous one-key-hidpi (Added in commit https://github.com/daliansky/XiaoMi-Pro-Hackintosh/commit/a3b7f136209a91455944b4afece7e14a931e62ba)
    sudo rm -rf $DISPLAYPATH/DisplayVendorID-9e5
    echo 'Remove complete'
    echo
}


# Create backup for Icons.plist
function backup() {
    echo 'Backing up...'
    sudo mkdir -p $DISPLAYPATH/backup
    sudo cp $DISPLAYPATH/Icons.plist $DISPLAYPATH/backup/
    echo 'Back up complete'
    echo
}

# Copy the display folder
function copy() {
    echo 'Copying file to target address...'
    sudo mkdir -p $DISPLAYPATH/DisplayVendorID-9e5
    sudo cp ./Icons.plist $DISPLAYPATH/
    sudo cp ./DisplayProductID-747 $DISPLAYPATH/DisplayVendorID-9e5/
    sudo cp ./DisplayProductID-747.tiff $DISPLAYPATH/DisplayVendorID-9e5/
    echo 'Copy complete'
    echo
}

# Fix permission
function fixpermission() {
    echo 'Fixing permission...'
    sudo chown root:wheel $DISPLAYPATH/Icons.plist
    sudo chown root:wheel $DISPLAYPATH/DisplayVendorID-9e5
    sudo chown root:wheel $DISPLAYPATH/DisplayVendorID-9e5/DisplayProductID-747
    sudo chown root:wheel $DISPLAYPATH/DisplayVendorID-9e5/DisplayProductID-747.tiff
    echo 'Fix complete'
    echo
}

# Clean
function clean() {
    echo 'Cleaning temporary files...'
    sudo rm -rf ../one-key-hidpi
    echo 'Clean complete'
    echo
}

function makedir() {
    mkdir -p $DISPLAYPATH
}

# Install
function install() {
    makedir
    download
    removeold
    backup
    copy
    fixpermission
    clean
    echo 'Wonderful! This is the end of the installation, please reboot and choose 1424x802 in SysPref! '
    exit 0
}

# Uninstall
function uninstall() {
    echo 'Uninstalling one-key-hidpi...'
    sudo rm -rf $DISPLAYPATH/DisplayVendorID-9e5

    # Restore Icon.plist in backup folder if presents
    if [ -f "$DISPLAYPATH/backup/Icons.plist" ];then
        sudo cp $DISPLAYPATH/backup/Icons.plist $DISPLAYPATH/
        sudo chown root:wheel $DISPLAYPATH/Icons.plist
    fi

    # Remove backup folder
    sudo rm -rf $DISPLAYPATH/backup
    echo 'Uninstall complete'
    exit 0
}

# Main function
function main() {
    read -n 1 -p "Input operation: 1 for install, 2 for uninstall" ops
    if (( $ops == 1 ))
    then
        install
    else
        uninstall
    fi
    exit 0
}

main
